package com.zjz.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {
    //由于get和post只是请求实现的不同方式，可以相互调用，业务逻辑都一样
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        ServletOutputStream outputStream = resp.getOutputStream();
//        PrintWriter writer = resp.getWriter();  //响应流
//
//        writer.println("Hello Servlet");
//        this.getInitParameter();  初始化参数
//        this.getServletConfig();   Servlet配置
//        this.getServletContext();  Servlet上下文

        ServletContext context = this.getServletContext();
        String username = "zjz";
        context.setAttribute("username",username);//将一个数据保存在了ServletContext中，名字为username，值为username

        resp.sendRedirect("success.jsp");


        /* //响应的类型：html
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        //获取响应的输出流
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>你 好!</h1>");
        out.println("</body>");
        out.println("</html>");*/
    }

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        doGet(req, resp);
//    }
}
